const Upload = require('../../libs/upload')

/**
 * 音乐模型
 */
class AblumDemo {
    /**
     * 构造音乐对象
     * @param  {number} id       标识
     * @param  {string} name     歌曲名
     * @param  {string} artist   艺术家
     * @param  {string} poster   海报文件名
     * @param  {string} publishDate    发布时间
     * @param  {number} playCount    播放次数
     * @param  {number} artist_id    歌手ID
     * @return {Music}           音乐对象
     */
    constructor(id, name, artist, poster, publishDate, playCount, artist_id) {
        this.id = id
        this.name = name
        this.artist = artist
        this.poster = poster
        this.publishDate = publishDate
        this.playCount = playCount
        this.artist_id = artist_id
    }

    static find(limit = 0) {
        if (limit > 0) {
            const listTemp = storage.sort(function(a, b) {
                return b.id - a.id
            })
            let list = listTemp.slice(0, limit)
        } else {
            let list = storage
        }

        const new_list = list.map(item => {
            const temp = {}
            Object.assign(temp, item)
                // lodash
            temp.poster = Upload.getUrl(temp.poster)
            return temp
        })

        return new_list
    }

    static findOne(id) {
        const albumDemo = storage.find(s => s.id === id)
        if (albumDemo) {
            const Album = {}
            Object.assign(Album, albumDemo)
            Album.poster = Upload.getUrl(Album.poster)
            return Album
        }
        return albumDemo
    }

    delete() {
        return storage.splice(storage.indexOf(this), 1)
    }

    save() {
        storage.indexOf(this) === -1 && storage.push(this)
        return true
    }

    update() {
        return true
    }
}

const storage = [
    new AblumDemo(1, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(2, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(3, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(4, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(5, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(6, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(7, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(8, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(9, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(10, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(11, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(12, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(13, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(14, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(15, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(16, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(17, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(18, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(19, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
    new AblumDemo(20, '中国范', '周杰伦', 'jyqj.png', '2017-09-11', 232023, 1),
]

module.exports = AblumDemo